<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/base/base.html">
<script>
'use strict';

/**
 * @fileoverview Provides a caching layer for elided text values.
 */
tr.exportTo('tr.ui.b', function() {
  /**
   * Cache for elided strings.
   * Moved from the ElidedTitleCache protoype to a "global" for speed
   * (variable reference is 100x faster).
   *   key: String we wish to elide.
   *   value: Another dict whose key is width
   *     and value is an ElidedStringWidthPair.
   */
  const elidedTitleCacheDict = new Map();
  const elidedTitleCache = new ElidedTitleCache();

  /**
   * A cache for elided strings.
   * @constructor
   */
  function ElidedTitleCache() {
    // TODO(jrg): possibly obsoleted with the elided string cache.
    // Consider removing.
    this.textWidthMap = new Map();
  }

  ElidedTitleCache.prototype = {
    /**
     * Return elided text.
     *
     * @param {ctx} Context The graphics context.
     * @param {pixWidth} Pixel width.
     * @param {title} Original title text.
     * @param {width} Drawn width in world coords.
     * @param {sliceDuration} Where the title must fit (in world coords).
     * @return {ElidedStringWidthPair} Elided string and width.
     */
    get(ctx, pixWidth, title, width, sliceDuration) {
      let elidedDict = elidedTitleCacheDict.get(title);
      if (!elidedDict) {
        elidedDict = new Map();
        elidedTitleCacheDict.set(title, elidedDict);
      }

      let elidedDictForPixWidth = elidedDict.get(pixWidth);
      if (!elidedDictForPixWidth) {
        elidedDict.set(pixWidth, new Map());
        elidedDictForPixWidth = elidedDict.get(pixWidth);
      }

      let stringWidthPair = elidedDictForPixWidth.get(sliceDuration);
      if (stringWidthPair === undefined) {
        let newtitle = title;
        let elided = false;
        while (this.labelWidthWorld(ctx, newtitle, pixWidth) > sliceDuration) {
          if (newtitle.length * 0.75 < 1) break;
          newtitle = newtitle.substring(0, newtitle.length * 0.75);
          elided = true;
        }

        if (elided && newtitle.length > 3) {
          newtitle = newtitle.substring(0, newtitle.length - 3) + '...';
        }

        stringWidthPair = new ElidedStringWidthPair(
            newtitle, this.labelWidth(ctx, newtitle));
        elidedDictForPixWidth.set(sliceDuration, stringWidthPair);
      }
      return stringWidthPair;
    },

    quickMeasureText_(ctx, text) {
      let w = this.textWidthMap.get(text);
      if (!w) {
        w = ctx.measureText(text).width;
        this.textWidthMap.set(text, w);
      }
      return w;
    },

    labelWidth(ctx, title) {
      return this.quickMeasureText_(ctx, title) + 2;
    },

    labelWidthWorld(ctx, title, pixWidth) {
      return this.labelWidth(ctx, title) * pixWidth;
    }
  };

  /**
   * A pair representing an elided string and world-coordinate width
   * to draw it.
   * @constructor
   */
  function ElidedStringWidthPair(string, width) {
    this.string = string;
    this.width = width;
  }

  return {
    ElidedTitleCache,
  };
});
</script>
